<template>
    <div class="contener">
        <div class="tree-page">
            <!-- 左侧树 -->
            <div class="tree-menu">
                <base-tree
                    :data="baseTreeConfig.data"
                    :loading="baseTreeConfig.loading"
                    :currentNodeKey="baseTreeConfig.currentNodeKey"
                    :filterShow="baseTreeConfig.filterShow"
                    @NodeClick="handelTreeClick"
                />
            </div>
            <!-- 页面 -->
            <div class="page">
                <!-- 搜索 -->
                <transition
                    @before-enter="_beforeEnter"
                    @enter="_enter"
                    @after-enter="_afterEnter"
                    @before-leave="_beforeLeave"
                    @leave="_leave"
                    @after-leave="_afterLeave"
                >
                    <div
                        class="search-contener"
                        ref="searchContener"
                        v-show="searchFormConfig.searchContenerShow"
                    >
                        <base-form
                            :formItems="searchFormConfig.formItems"
                            :formData="searchFormConfig.formSubmit"
                            ref="searchForm"
                            @onButtonClick="searchBarButtonClick"
                        />
                    </div>
                </transition>
                <!-- 表格 -->
                <div class="datas-contener">
                    <!-- 表格操作项 -->
                    <div class="option-contener">
                        <data-options
                            @buttonClick="handelOptionClick"
                            :leftConfig="dataOptionConfig.leftConfig"
                        />
                    </div>
                    <!-- 表格展示项 -->
                    <div class="table-contener">
                        <base-table
                            ref="baseTable"
                            :tableData="baseTableConfig.tableData"
                            :tableHeader="baseTableConfig.tableHeader"
                            :tableConfig="baseTableConfig.tableConfig"
                            @handleSelection="tableDataSelected"
                        >
                            <template slot-scope="{ scope }">
                                <span v-if="scope.type === 'index'">
                                    {{ scope.$index + 1 }}
                                </span>
                                <span v-if="scope.type === 'source'">
                                    {{
                                        scope.row.datasourceId
                                            | getAttrName(
                                                dataSourceList,
                                                'value',
                                                'label'
                                            )
                                    }}
                                </span>
                                <div v-if="scope.type === 'subTableInfo'">
                                    <div
                                        class="subTableInfo"
                                        v-if="scope.row.subTableName"
                                    >
                                        <el-link
                                            type="primary"
                                            v-for="(item, index) in (
                                                scope.row.subTableName +
                                                ':' +
                                                scope.row.subTableComment +
                                                ':' +
                                                scope.row.subTableFkName
                                            ).split(',')"
                                            :key="index"
                                            @click="
                                                configModelColumn(
                                                    scope.row,
                                                    'sub'
                                                )
                                            "
                                            >{{ item }}</el-link
                                        >
                                    </div>
                                </div>
                                <el-link
                                    v-if="scope.type === 'tableInfo'"
                                    type="primary"
                                    @click="
                                        configModelColumn(scope.row, 'main')
                                    "
                                    v-hasPermi="[
                                        '/tool/dmd/businessModel/configColumn'
                                    ]"
                                >
                                    {{ scope.row.tableName }}
                                </el-link>
                                <div v-if="scope.type === 'handle'">
                                    <el-button
                                        type="text"
                                        @click.stop="deleteTableData(scope.row)"
                                        v-hasPermi="[
                                            '/tool/dmd/businessModel/delete'
                                        ]"
                                        >删除模型</el-button
                                    >
                                </div>
                            </template>
                        </base-table>
                    </div>
                    <!-- 分页 -->
                    <div class="paging-contener">
                        <pagin-ation
                            :size="paginationConfig.size"
                            :total="paginationConfig.total"
                            :type="paginationConfig.type"
                            :currentPage="paginationConfig.currentPage"
                            @PageNumber="pageNumberChange"
                            @PageSize="pageSizeChange"
                        />
                    </div>
                </div>
            </div>
        </div>
        <!-- 弹框 -->
        <el-dialog
            :title="dialogConfig.title"
            :visible.sync="dialogConfig.show"
            :width="dialogConfig.width"
            v-dialogDrag
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            append-to-body
        >
            <!-- 筛选表格列 -->
            <div
                class="dialog-content"
                v-if="dialogConfig.key === 'tableColumn'"
            >
                <base-transfer
                    :transferConfig="tableColumnTransferConfig"
                    ref="baseTransfer"
                />
            </div>
            <!-- 新增 -->
            <div
                class="dialog-content"
                v-if="dialogConfig.key === 'addTableData'"
            >
                <base-form
                    :formItems="addFormConfig.formItems"
                    :formData="addFormConfig.formSubmit"
                    :formConfig="addFormConfig.formConfig"
                    :formRules="addFormConfig.formRule"
                    @onRadioChange="handelRadioChange"
                    ref="addForm"
                    @onButtonClick="addFormButtonClick"
                    @onSelectChange="handerOnSelectChange"
                />
                <div class="add-table-contener">
                    <base-table
                        v-if="addFormConfig.formSubmit.busModelType === '1'"
                        ref="subTableParamTable"
                        :tableData="subTableParamTableConfig.tableData"
                        :tableHeader="subTableParamTableConfig.tableHeader"
                        :tableConfig="subTableParamTableConfig.tableConfig"
                    >
                        <template slot-scope="{ scope }">
                            <el-select
                                v-if="scope.type === 'subTableFkNameSelect'"
                                v-model="scope.row.subTableFkName"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in scope.row.columns"
                                    :key="item.columnName"
                                    :label="
                                        item.columnName + ':' + item.remarks
                                    "
                                    :value="item.columnName"
                                />
                            </el-select>
                        </template>
                    </base-table>
                </div>
            </div>
            <!-- 模型配置 -->
            <div
                class="config—model-dialog"
                v-if="dialogConfig.key === 'configModelData'"
            >
                <base-table
                    ref="sconfigModelTable"
                    :tableData="configBusModelTableConfig.tableData"
                    :tableHeader="configBusModelTableConfig.tableHeader"
                    :tableConfig="configBusModelTableConfig.tableConfig"
                >
                    <template slot-scope="{ scope }">
                        <el-checkbox
                            v-if="scope.type === 'isRequiredCheckBox'"
                            v-model="scope.row.required"
                        />
                        <el-checkbox
                            v-if="scope.type === 'isListCheckBox'"
                            v-model="scope.row.list"
                        />
                        <el-checkbox
                            v-if="scope.type === 'isInsertCheckBox'"
                            v-model="scope.row.insert"
                        />
                        <el-checkbox
                            v-if="scope.type === 'isEditCheckBox'"
                            v-model="scope.row.edit"
                        />
                        <el-checkbox
                            v-if="scope.type === 'isQueryCheckBox'"
                            v-model="scope.row.query"
                        />
                        <el-select
                            v-if="scope.type === 'queryTypeSelect'"
                            v-model="scope.row.queryType"
                            placeholder="请选择"
                        >
                            <el-option
                                v-for="item in queryTypeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                        <el-select
                            v-if="scope.type === 'htmlTypeSelect'"
                            v-model="scope.row.htmlType"
                            placeholder="请选择"
                        >
                            <el-option
                                v-for="item in htmlTypeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                        <el-select
                            v-if="scope.type === 'dictTypeSelect'"
                            v-model="scope.row.dictType"
                            placeholder="请选择"
                        >
                            <el-option
                                v-for="item in dictTypeList"
                                :key="item.type"
                                :label="item.name"
                                :value="item.type"
                            />
                        </el-select>
                    </template>
                </base-table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="handelCloseDialog"
                    >取 消</el-button
                >
                <el-button
                    size="small"
                    type="primary"
                    @click="handelCofimeDialog"
                    >确 定</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>
<script>
import BaseTree from '@/components/baseTree/index'
import BaseForm from '@/components/baseForm/index'
import DataOptions from '@/components/dataOptions/index'
import BaseTable from '@/components/baseTable/index'
import PaginAtion from '@/components/pagination/index'
import BaseTransfer from '@/components/baseTransfer/index'
import globalMixin from '@/mixins/global'
import { busModelTypeTree } from '@/http/apis/tool/dmd/type'
import { getDataSourceSelectData } from '@/http/apis/system/dataSource/index'
import {
    businessModelList,
    createBusinessModel,
    delBusinessModel,
    getTables,
    getColumns,
    getGebTableColumns,
    configColumn
} from '@/http/apis/tool/dmd/busModel'
import { optionselect } from '@/http/apis/system/dict/type'
import { getDictDatas } from '@/plugins/dict'
import {
    assemblyDataByTableColumn,
    setFormItemProp,
    setSelectFormart
} from '@/utils/tool'
export default {
    components: {
        BaseTree,
        BaseForm,
        DataOptions,
        BaseTable,
        PaginAtion,
        BaseTransfer
    },
    mixins: [globalMixin],
    dicts: ['sys_normal_disable', 'sys_user_sex'],
    data() {
        return {
            busModelTypeList: [
                { value: '0', name: '单表' },
                { value: '1', name: '复合表' }
            ],
            //查询方式列表
            queryTypeList: [
                { value: '=', label: 'EQ' },
                { value: '!=', label: 'NE' },
                { value: '>', label: 'GT' },
                { value: '>=', label: 'GTE' },
                { value: '<', label: 'LT' },
                { value: '<=', label: 'LTE' },
                { value: 'LIKE', label: 'LIKE' },
                { value: 'BETWEEN', label: 'BETWEEN' }
            ],
            //显示方式列表
            htmlTypeList: [
                { value: 'none', label: '隐藏' },
                { value: 'input', label: '文本框' },
                { value: 'textarea', label: '文本域' },
                { value: 'select', label: '下拉框' },
                { value: 'radio', label: '单选框' },
                { value: 'checkbox', label: '复选框' },
                { value: 'datetime', label: '日期控件' },
                { value: 'editor', label: '富文本控件' }
            ],
            //数据字典列表
            dictTypeList: [],
            //数据源数组
            dataSourceList: [],
            //数据表列表
            tableList: [],
            //表字段列表
            columnList: [],
            // 通用树组件配置
            baseTreeConfig: {
                // tree的数据
                data: [],
                // tree的加载状态
                loading: false,
                // tree选中的树
                currentNodeKey: '',
                // 过滤显示
                filterShow: true
            },
            // 搜索表单配置
            searchFormConfig: {
                formItems: [
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入模型名称',
                            label: '模型名称',
                            prop: 'functionName',
                            width: 'w-2'
                        },
                        {
                            ltype: 'input',
                            placeholder: '请输入表名称',
                            label: '表名称',
                            prop: 'tableName',
                            width: 'w-2'
                        },
                        {
                            ltype: 'select',
                            placeholder: '请选择数据源',
                            label: '数据源',
                            prop: 'datasourceId',
                            width: 'w-2',
                            options: getDictDatas('sys_normal_disable')
                        },
                        {
                            ltype: 'datePicker',
                            type: 'daterange',
                            placeholder: '请选择创建时间',
                            label: '创建时间',
                            prop: 'timeRange',
                            width: 'w-3',
                            startPlaceholder: '请选择开始时间',
                            endPlaceholder: '请选择结束时间',
                            valueFormat: 'yyyy-MM-dd'
                        },
                        {
                            ltype: 'button',
                            type: 'primary',
                            text: '搜索',
                            prop: 'searchDatas',
                            icon: 'el-icon-search',
                            width: 'w-auto',
                            loading: false
                        },
                        {
                            ltype: 'button',
                            type: 'default',
                            text: '重置',
                            prop: 'resetSearchForm',
                            icon: 'el-icon-refresh-right',
                            width: 'w-auto'
                        }
                    ]
                ],
                formSubmit: {
                    functionName: '',
                    tableName: '',
                    datasourceId: '',
                    timeRange: '',
                    groupId: ''
                },
                formConfig: {
                    labelPosition: 'right', // label对齐方式
                    labelWidth: 'auto'
                },
                // 搜索控件是否显示
                searchContenerShow: true
            },
            // 数据操作项配置
            dataOptionConfig: {
                // 左侧按钮配置
                leftConfig: [
                    {
                        prop: 'addTableData',
                        label: '创建模型',
                        icon: 'el-icon-plus',
                        type: 'primary',
                        disabled: false,
                        hasPermi: ['/tool/dmd/businessModel/create']
                    }
                ]
            },
            // 表格配置
            baseTableConfig: {
                // 表格数据
                tableData: [],
                // 表格头数据
                tableHeader: [
                    {
                        type: 'selection',
                        width: 50,
                        fixed: 'left',
                        label: '选择',
                        visible: true
                    },
                    {
                        prop: 'functionName',
                        label: '模型名称',
                        visible: true
                    },
                    {
                        type: 'tableInfo',
                        prop: 'tableName',
                        label: '表名称',
                        visible: true
                    },
                    {
                        prop: 'tableComment',
                        label: '表描述',
                        visible: true
                    },
                    {
                        type: 'subTableInfo',
                        label: '关联子表信息',
                        width: 400,
                        visible: true
                    },
                    {
                        type: 'source',
                        label: '数据源',
                        visible: true
                    },
                    {
                        prop: 'createTime',
                        label: '创建时间',
                        showOverflowTooltip: true,
                        visible: true
                    },
                    {
                        label: '操作',
                        type: 'handle',
                        fixed: 'right',
                        visible: true
                    }
                ],
                // 表格配置
                tableConfig: {
                    loading: false
                },
                // 选中的表格数据
                tableCheckedDatas: []
            },

            // 子表配置
            subTableParamTableConfig: {
                // 表格数据
                tableData: [],
                // 表格头数据
                tableHeader: [
                    {
                        prop: 'subTableName',
                        label: '子表名称',
                        visible: true
                    },
                    {
                        prop: 'subTableCommont',
                        label: '子表描述',
                        visible: true
                    },
                    {
                        type: 'subTableFkNameSelect',
                        prop: 'subTableFkName',
                        label: '关联外键',
                        visible: true
                    }
                ],
                // 表格配置
                tableConfig: {
                    loading: false,
                    border: true
                }
            },
            // 分页配置
            paginationConfig: {
                type: 'md',
                size: 10,
                currentPage: 1,
                total: 100
            },
            // 弹框配置
            dialogConfig: {
                // 弹框标题
                title: '新增',
                // 是否显示
                show: false,
                // 宽度
                width: 'auto',
                // 弹框标识(用来区分弹框内容)
                key: ''
            },
            // 查询表格数据的参数
            queryTableParams: {
                pageNo: 0,
                pageSize: 10,
                entity: {}
            },
            // 表格列筛选配置
            tableColumnTransferConfig: {
                // 穿梭框的数据
                data: [],
                // 穿梭框的标题
                titles: ['显示', '隐藏'],
                // 隐藏的列
                modelList: []
            },
            // 新增表单配置
            addFormConfig: {
                // 表单项
                formItems: [
                    [
                        {
                            ltype: 'input',
                            placeholder: '请输入模型名称',
                            label: '模型名称',
                            prop: 'functionName',
                            width: 'w-1'
                        },
                        {
                            ltype: 'radio',
                            label: '模型类型',
                            prop: 'busModelType',
                            width: 'w-1',
                            options: []
                        }
                    ],
                    [
                        {
                            ltype: 'treeSelect',
                            placeholder: '请选择所属分类',
                            label: '所属分类',
                            prop: 'groupId',
                            width: 'w-1',
                            multiple: false,
                            options: []
                        },
                        {
                            ltype: 'select',
                            placeholder: '请选择数据源',
                            label: '数据源',
                            prop: 'datasourceId',
                            multiple: false,
                            width: 'w-1',
                            options: []
                        }
                    ],
                    [
                        {
                            ltype: 'select',
                            label: '主表名称',
                            prop: 'tableInfoStr',
                            width: 'w-1',
                            multiple: false,
                            options: []
                        },
                        {
                            ltype: 'select',
                            placeholder: '请选择子表',
                            label: '子表名称',
                            prop: 'subTableInfos',
                            multiple: true,
                            isShow: false,
                            options: []
                        }
                    ]
                ],
                // 提交项
                formSubmit: {
                    functionName: '',
                    busModelType: '0',
                    groupId: null,
                    datasourceId: '',
                    tableInfoStr: '',
                    subTableInfos: []
                },
                // 配置项
                formConfig: {
                    labelPosition: 'right', // label对齐方式
                    labelWidth: '80px'
                },
                // 验证项
                formRule: {
                    functionName: [
                        {
                            required: true,
                            message: '请输入模型名称',
                            trigger: 'blur'
                        }
                    ],
                    groupId: [
                        {
                            required: true,
                            message: '请选择所属分类',
                            trigger: 'blur'
                        }
                    ],
                    datasourceId: [
                        {
                            required: true,
                            message: '请选择数据源',
                            trigger: 'blur'
                        }
                    ],
                    tableInfoStr: [
                        {
                            required: true,
                            message: '请选择主表',
                            trigger: 'blur'
                        }
                    ],
                    subTableInfos: [
                        {
                            required: false,
                            message: '请选择子表',
                            trigger: 'blur'
                        }
                    ]
                }
            },
            //模型配置表格
            configBusModelTableConfig: {
                // 表格数据
                tableData: [],
                // 表格头数据
                tableHeader: [
                    {
                        prop: 'columnName',
                        label: '列名称',
                        width: 200,
                        visible: true
                    },
                    {
                        prop: 'columnComment',
                        label: '列描述',
                        showOverflowTooltip: true,
                        visible: true
                    },
                    {
                        prop: 'javaType',
                        label: 'Java类型',
                        width: 80,
                        visible: true
                    },
                    {
                        prop: 'javaField',
                        label: 'java属性',
                        visible: true
                    },
                    {
                        type: 'isInsertCheckBox',
                        prop: 'insert',
                        label: '插入',
                        width: 60,
                        visible: true
                    },
                    {
                        type: 'isEditCheckBox',
                        prop: 'edit',
                        label: '编辑',
                        width: 60,
                        visible: true
                    },
                    {
                        type: 'isListCheckBox',
                        prop: 'list',
                        label: '列表',
                        width: 60,
                        visible: true
                    },
                    {
                        type: 'isQueryCheckBox',
                        prop: 'query',
                        label: '查询',
                        width: 60,
                        visible: true
                    },
                    {
                        type: 'queryTypeSelect',
                        prop: 'queryType',
                        label: '查询条件',
                        visible: true
                    },
                    {
                        type: 'isRequiredCheckBox',
                        prop: 'required',
                        label: '必填',
                        width: 60,
                        visible: true
                    },
                    {
                        prop: 'htmlType',
                        type: 'htmlTypeSelect',
                        label: '显示类型',
                        visible: true
                    },
                    {
                        label: '字典类型',
                        type: 'dictTypeSelect',
                        prop: 'dictType',
                        visible: true
                    }
                ],
                // 表格配置
                tableConfig: {
                    loading: false
                }
            }
        }
    },
    created() {
        this.initPage()
    },
    methods: {
        /**
         * 页面开始
         */
        async initPage() {
            // 查询数据模型分类树
            await this.queryDataModelTypeTree()
            // 查询数据源列表
            await this.queryDataSourceList()
            //查询数据字典
            await this.queryDictTypeList()
            // 设置用户表单的配置项
            this.setFormsItems()
            // 查询用户列表
            this.queryTableData(this.queryTableParams)
        },
        /**
         * 左侧部门树相关
         */
        /**
         * 查询数据模型分类树
         */
        async queryDataModelTypeTree() {
            this.baseTreeConfig.loading = true
            try {
                let res = await busModelTypeTree()
                if (res && res.data) {
                    this.baseTreeConfig.data = res.data
                } else {
                    this.baseTreeConfig.data = []
                }
                this.baseTreeConfig.loading = false
            } catch (error) {
                this.baseTreeConfig.loading = false
            }
        },
        /**
         * 查询数据源列表
         */
        async queryDataSourceList() {
            try {
                let res = await getDataSourceSelectData()
                this.dataSourceList = res.data
            } catch (error) {
                this.dataSourceList = []
            }
        },
        /**
         * 根据数据源查询数据源下的表
         */
        async getTablesByDs(datasourceId) {
            try {
                let params = {
                    datasourceId: datasourceId
                }
                let res = await getTables(params)
                this.tableList = res.data
                this.tableList.forEach(v => {
                    v.showName = v.tableName + ':' + v.remarks
                })
            } catch (error) {
                this.tableList = []
            }
        },
        /**
         * 根据数据源he表名查询表的字段
         */
        async getColumnsByDsAndTable(datasourceId, tableName) {
            this.columnList = []
            try {
                let params = {
                    datasourceId: datasourceId,
                    tableName: tableName
                }
                let res = await getColumns(params)
                this.columnList = res.data
            } catch (error) {
                this.columnList = []
            }
        },
        /**
         * 查询模型列数据
         * @param {*} tableId
         * @param {*} tableName
         */
        async queryBusModelColumns(tableId, tableName) {
            try {
                let params = {
                    tableId: tableId,
                    tableName: tableName
                }
                let res = await getGebTableColumns(params)
                this.configBusModelTableConfig.tableData = res.data
            } catch (error) {
                this.configBusModelTableConfig.tableData = []
            }
        },
        /**
         * 查询数据字典
         */
        async queryDictTypeList() {
            try {
                let res = await optionselect()
                this.dictTypeList = res.data
            } catch (error) {
                this.dictTypeList = []
            }
        },
        /**
         * 树点击
         * @param {Object} params
         */
        handelTreeClick(params) {
            // 初始化查询条件
            this.initQueryParmas()
            this.baseTreeConfig.currentNodeKey = params.item.id
            // 组装查询条件
            this.queryTableParams.entity = {
                groupId: params.item.id
            }
            // 查询数据
            this.queryTableData(this.queryTableParams)
        },

        /**
         * 中间表格相关
         */

        /**
         * 查询表格数据
         */
        async queryTableData(params) {
            this.baseTableConfig.tableConfig.loading = true
            try {
                let res = await businessModelList(params)
                if (res.data) {
                    this.baseTableConfig.tableData = res.data.list
                    this.paginationConfig.total = res.data.totalCount
                } else {
                    this.baseTableConfig.tableData = []
                    this.paginationConfig.total = 0
                }
                this.baseTableConfig.tableConfig.loading = false
            } catch (error) {
                this.baseTableConfig.tableConfig.loading = false
            }
        },
        /**
         * 表格数据被选中
         */
        tableDataSelected(rows) {
            this.baseTableConfig.tableCheckedDatas = rows
        },
        /**
         * 初始化查询参数
         */
        initQueryParmas() {
            this.queryTableParams = {
                pageNo: 0,
                pageSize: 10,
                entity: {
                    groupId: this.baseTreeConfig.currentNodeKey
                }
            }
            this.paginationConfig.size = 10
            this.paginationConfig.currentPage = 1
            this.baseTableConfig.tableData = []
        },
        /**
         * 页码变化
         */
        pageNumberChange(e) {
            this.queryTableParams.pageNo = e
            this.paginationConfig.currentPage = e
            this.queryTableData(this.queryTableParams)
        },
        /**
         * 页大小变化
         */
        pageSizeChange(e) {
            // 页大小变化时，将当前页码设置为0
            this.queryTableParams.pageNo = 0
            // 将当前表格数据设置为空
            this.baseTableConfig.tableData = []
            this.queryTableParams.pageSize = e
            this.queryTableData(this.queryTableParams)
        },

        /**
         * 搜索栏相关
         */
        /**
         * 搜索栏按钮点击
         * @param {Object} params
         */
        searchBarButtonClick(params) {
            // 清空查询参数
            this.initQueryParmas()
            try {
                this[params.prop]()
            } catch (error) {
                throw new Error(error)
            }
        },
        /**
         * 重置搜索表单
         */
        resetSearchForm() {
            // 查询参数初始化
            this.initQueryParmas()
            // 重置搜索表单
            this.$refs.searchForm.resetForm()
            // 查询数据
            this.refreshTableData()
        },

        /**
         * 搜索数据
         */
        searchDatas() {
            this.$refs.searchForm.submitForm(() => {
                // 清空查询参数
                this.initQueryParmas()
                // 组装查询条件
                this.queryTableParams.entity = {
                    ...this.searchFormConfig.formSubmit
                }
                // 查询数据
                this.queryTableData(this.queryTableParams)
            })
        },

        /**
         * 操作栏相关
         */
        /**
         * 操作项点击
         * @param {*} params
         */
        handelOptionClick(params) {
            this[params] ? this[params]() : null
        },
        /**
         * 切换搜索栏的显示状态
         */
        switchSearchStatus() {
            this.searchFormConfig.searchContenerShow = !this.searchFormConfig
                .searchContenerShow
        },
        /**
         * 刷新表格数据
         */
        refreshTableData() {
            // 将当前表格数据设置为空
            this.baseTableConfig.tableData = []
            this.queryTableData(this.queryTableParams)
        },
        /**
         * 设置表格显示隐藏列(打开弹框)
         */
        switchTableColumn() {
            let dialogParams = {
                title: '显示/隐藏',
                width: 'auto',
                show: true,
                key: 'tableColumn'
            }
            // 设置参与表格列控制的属性（最后的数组为不参与隐藏显示设置的列）
            this.tableColumnTransferConfig.data = assemblyDataByTableColumn(
                this.baseTableConfig.tableHeader,
                'label',
                ['选择', '用户名称', '操作']
            )
            this.handelOpenDialog(dialogParams)
        },
        /**
         * 设置表格显示隐藏列(弹框确认)
         */
        switchTableColumnButtonClick() {
            let newData = this.$refs.baseTransfer.backCheckedData()
            if (newData && newData.length > 0) {
                this.baseTableConfig.tableHeader.forEach(ele => {
                    ele.visible = !newData.includes(ele.label)
                })
            } else {
                this.baseTableConfig.tableHeader.forEach(ele => {
                    ele.visible = true
                })
            }
            this.handelCloseDialog()
        },
        /**
         * 设置(新增、修改)表单的表单项
         */
        async setFormsItems() {
            // 重新设置新增、修改 表单的部门树数据配置
            let dataSourceSelectData = setSelectFormart(
                this.dataSourceList,
                'datasourceName',
                'datasourceId'
            )
            this.searchFormConfig.formItems = setFormItemProp(
                this.searchFormConfig.formItems,
                'datasourceId',
                'options',
                dataSourceSelectData
            )
            this.addFormConfig.formItems = setFormItemProp(
                this.addFormConfig.formItems,
                'datasourceId',
                'options',
                dataSourceSelectData
            )
            this.addFormConfig.formItems = setFormItemProp(
                this.addFormConfig.formItems,
                'busModelType',
                'options',
                this.busModelTypeList
            )
            this.addFormConfig.formItems = setFormItemProp(
                this.addFormConfig.formItems,
                'groupId',
                'options',
                this.baseTreeConfig.data
            )
        },
        /**
         * 新增表格数据(打开弹框)
         */
        addTableData() {
            // 打开新增弹框
            let dialogParams = {
                title: '新增',
                width: '40%',
                show: true,
                key: 'addTableData'
            }
            this.handelOpenDialog(dialogParams)
            this.$nextTick(() => {
                this.initAddForm()
            })
        },
        /**
         * 新增数据确认提交(弹框确认)
         */
        addFormButtonClick() {
            this.$refs['addForm'].submitForm(async res => {
                if (res) {
                    res.tableName = res.tableInfoStr.split(':')[0]
                    res.tableCommont = res.tableInfoStr.split(':')[1]
                    var subTableName = ''
                    var subTableCommont = ''
                    var subTableFkName = ''
                    this.subTableParamTableConfig.tableData.forEach(v => {
                        if (subTableName === '') {
                            subTableName = v.subTableName
                        } else {
                            subTableName = subTableName + ',' + v.subTableName
                        }
                        if (subTableCommont === '') {
                            subTableCommont = v.subTableCommont
                        } else {
                            subTableCommont =
                                subTableCommont + ',' + v.subTableCommont
                        }
                        if (subTableFkName === '') {
                            subTableFkName = v.subTableFkName
                        } else {
                            subTableFkName =
                                subTableFkName + ',' + v.subTableFkName
                        }
                    })
                    res.subTableName = subTableName
                    res.subTableCommont = subTableCommont
                    res.subTableFkName = subTableFkName
                    let assRes = await createBusinessModel(res)
                    if (assRes && assRes.code === 0) {
                        this.$message.closeAll()
                        this.$message.success({ message: '新增成功！' })
                    } else {
                        this.$message.closeAll()
                        this.$message.error({ message: '新增失败！' })
                    }
                    this.handelCloseDialog()
                    // 查询数据
                    this.refreshTableData()
                    this.initAddForm()
                } else {
                    this.initAddForm()
                }
            })
        },
        /**
         * 初始化新增表单
         */
        initAddForm() {
            this.addFormConfig.formSubmit = {
                functionName: '',
                busModelType: '0',
                groupId: null,
                datasourceId: '',
                tableInfoStr: '',
                subTableInfos: []
            }
            this.$refs.addForm.resetForm()
        },
        /**
         * 配置模型(打开弹框)
         */
        async configModelColumn(row, type) {
            let tableId = row.tableId
            let tableName = ''
            if (type === 'main') {
                tableName = row.tableName
            } else {
                tableName = row.subTableName
            }
            await this.queryBusModelColumns(tableId, tableName)
            // 打开修改弹框
            let dialogParams = {
                title: '配置模型',
                width: '70%',
                show: true,
                key: 'configModelData'
            }
            this.handelOpenDialog(dialogParams)
        },
        /**
         * 配置模型数据提交(弹框确认)
         */
        async configModelFormButtonClick() {
            this.configBusModelTableConfig.tableData.forEach(v => {
                v.isRequired = v.required ? '1' : '0'
                v.isQuery = v.query ? '1' : '0'
                v.isList = v.list ? '1' : '0'
                v.isInsert = v.insert ? '1' : '0'
                v.isEdit = v.edit ? '1' : '0'
            })
            let res = this.configBusModelTableConfig.tableData
            let assRes = await configColumn(res)
            if (assRes && assRes.code === 0) {
                this.$message.closeAll()
                this.$message.success({ message: '配置成功！' })
            } else {
                this.$message.closeAll()
                this.$message.error({ message: '配置失败！' })
            }
            this.handelCloseDialog()
            // 查询数据
            this.refreshTableData()
            this.initUpdateForm()
        },
        /**
         * 初始化修改表单
         */
        initUpdateForm() {
            this.updateFormConfig.formSubmit = {}
        },
        /**
         * 删除表格数据
         */
        deleteTableData(rows) {
            if (
                typeof rows === 'undefined' &&
                this.baseTableConfig.tableCheckedDatas.length > 0
            ) {
                rows = this.baseTableConfig.tableCheckedDatas
            }
            if (typeof rows !== 'undefined') {
                let params = []
                if (Array.isArray(rows) && rows.length) {
                    for (let i = 0; i < rows.length; i++) {
                        const element = rows[i]
                        params.push(element.tableId)
                    }
                } else {
                    params = [rows.tableId]
                }
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(async () => {
                        let res = await delBusinessModel(params)
                        if (res && res.code == 0) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            })
                        }
                        // 查询数据
                        this.refreshTableData()
                    })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        })
                    })
            } else {
                this.$message({
                    type: 'error',
                    message: '请选择数据！'
                })
            }
        },

        /**
         * 弹框相关
         */
        /**
         * 打开弹框
         * @param {*} params
         */
        handelOpenDialog(params) {
            this.dialogConfig = params
        },
        /**
         * 初始化弹框
         */
        handelInitDialog() {
            this.dialogConfig = {
                title: '',
                show: false,
                width: '',
                key: ''
            }
        },
        /**
         * 关闭弹框
         */
        handelCloseDialog() {
            this.handelInitDialog()
        },
        /**
         * 弹框确定
         */
        handelCofimeDialog() {
            // 表格列筛选的弹框
            if (this.dialogConfig.key === 'tableColumn') {
                this.switchTableColumnButtonClick()
            } else if (this.dialogConfig.key === 'addTableData') {
                this.addFormButtonClick()
            } else if (this.dialogConfig.key === 'configModelData') {
                this.configModelFormButtonClick()
            }
        },
        //表单单选组件监听事件
        handelRadioChange(params) {
            if (params.prop === 'busModelType') {
                if (params.data === 0) {
                    this.addFormConfig.formRule.subTableInfos[0].required = false
                    setFormItemProp(
                        this.addFormConfig.formItems,
                        'subTableInfos',
                        'isShow',
                        false
                    )
                } else {
                    this.addFormConfig.formRule.subTableInfos[0].required = true
                    setFormItemProp(
                        this.addFormConfig.formItems,
                        'subTableInfos',
                        'isShow',
                        true
                    )
                }
            }
        },
        async handerOnSelectChange(params) {
            if (params.prop === 'datasourceId') {
                if (params.data !== undefined) {
                    await this.getTablesByDs(params.data)
                    let tableSelectData = setSelectFormart(
                        this.tableList,
                        'showName',
                        'showName'
                    )
                    setFormItemProp(
                        this.addFormConfig.formItems,
                        'tableInfoStr',
                        'options',
                        tableSelectData
                    )
                    setFormItemProp(
                        this.addFormConfig.formItems,
                        'subTableInfos',
                        'options',
                        tableSelectData
                    )
                }
            } else if (params.prop === 'tableInfoStr') {
                let tableSelectData = setSelectFormart(
                    this.tableList,
                    'showName',
                    'showName'
                )
                setFormItemProp(
                    this.addFormConfig.formItems,
                    'subTableInfos',
                    'options',
                    tableSelectData
                )
            } else if (params.prop === 'subTableInfos') {
                this.subTableParamTableConfig.tableData = []
                params.data.forEach(async v => {
                    await this.getColumnsByDsAndTable(
                        this.addFormConfig.formSubmit.datasourceId,
                        v.split(':')[0]
                    )
                    this.subTableParamTableConfig.tableData.push({
                        subTableName: v.split(':')[0],
                        subTableCommont: v.split(':')[1],
                        subTableFkName: '',
                        columns: this.columnList
                    })
                })
            }
        }
    }
}
</script>
<style lang="scss" scoped>
.subTableInfo {
    @include box_flex(
        $width: 100%,
        $height: auto,
        $direction: column,
        $justify: flex-start,
        $align: center
    );
}
.dialog-content {
    @include box_flex(
        $width: 100%,
        $height: auto,
        $direction: column,
        $justify: flex-start,
        $align: center
    );
    .add-table-contener {
        @include box_flex(
            $width: 100%,
            $height: 40vh,
            $direction: column,
            $justify: flex-start,
            $align: center
        );
    }
}
.config—model-dialog {
    @include box_flex(
        $width: 100%,
        $height: 60vh,
        $direction: column,
        $justify: flex-start,
        $align: center
    );
}
</style>
